package com.dreamer.view.pyramid;

import com.dreamer.domain.pmall.order.CashStatus;
import com.dreamer.domain.pyramid.CashWithdrawal;
import com.dreamer.service.pyramid.CashWithdrawalHandler;
import com.dreamer.util.ExcelFile;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ps.mx.otter.utils.SearchParameter;
import ps.mx.otter.utils.WebUtil;
import ps.mx.otter.utils.message.Message;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping("/cash")
public class CashQueryController {

    @Autowired
    private CashWithdrawalHandler cashWithdrawalHandler;

    @RequestMapping("/index.html")
    public String index(
            @ModelAttribute("parameter") SearchParameter<CashWithdrawal> parameter,
            HttpServletRequest request, Model model) {
        try {
            List<CashWithdrawal> list = cashWithdrawalHandler.searchAllByPage(parameter);
            WebUtil.turnPage(parameter, request);
            model.addAttribute("list", list);
            model.addAttribute("CashStatus", CashStatus.values());
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        return "pyramid/cash_index1";
    }

    @RequestMapping(value = "/download.html")
    public void download(
            @ModelAttribute("parameter") SearchParameter<CashWithdrawal> parameter,
            HttpServletRequest request, HttpServletResponse response,
            Model model) {
        try {
            List<CashWithdrawal> list = cashWithdrawalHandler.searchAllByPage(parameter);
            Map<String, Integer> sum = new HashedMap();
            List<String> headers = new ArrayList<>();
            headers.add("用户名");
            headers.add("手机号");
            headers.add("银行卡号");
            headers.add("银行名称");
            headers.add("支行信息");
            headers.add("预留手机号");
            headers.add("提现金额");
            headers.add("申请时间");
            headers.add("状态");
            headers.add("处理人");
            headers.add("处理时间");
            headers.add("未通过原因");
            List<Map> datas = new ArrayList<>();
            Map m = null;
            CashWithdrawal cw = null;
            for (int i = 0; i < list.size(); i++) {
                cw = list.get(i);
                m = new HashedMap();
                m.put(0, cw.getUser().getRealName());
                m.put(1, cw.getUser().getMobile());
                m.put(2, cw.getCardNumber());
                m.put(3, cw.getBankName());
                m.put(4, cw.getSubBranch());
                m.put(5, cw.getPhone());
                m.put(6, cw.getAmountMoney());
                m.put(7, cw.getCreateTime());
                m.put(8, cw.getStatus().getName());
                m.put(9, cw.getName());
                m.put(10, cw.getHandleTime());
                m.put(11, cw.getReason());
                datas.add(m);
            }
            //总数表格
            List<String> sheaders=new ArrayList<>();
            Map ms=null;
            List<Map> sdatas=new ArrayList<>();
            for(String key:sum.keySet()){
                ms =new org.apache.commons.collections.map.HashedMap();
                ms.put(0,key);
                ms.put(1,""+sum.get(key));
                sdatas.add(ms);
            }
            List<String> ss=new ArrayList<>();
            List<List> hs=new ArrayList<>();
            hs.add(headers);
            hs.add(sheaders);
            List<List<Map>> ds=new ArrayList<>();
            ds.add(datas);
            ds.add(sdatas);
            ss.add("提现数据");
            ExcelFile.ExpExs("",ss,hs,ds,response);//创建表格并写入

        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }

//    @RequestMapping("/index.html")
//    public String index(Model model, @ModelAttribute("parameter") SearchParameter<CashVo> parameter) {
//        try {
//            List<CashVo> list = cashWithdrawalHandler.findCashVoAll(parameter);
//            model.addAttribute("CashVoList", list);
//        } catch (Exception exp) {
//            exp.printStackTrace();
//        }
//        return "pyramid/cash_index";
//    }
//
//    @ModelAttribute("parameter")
//    public SearchParameter<CashVo> preprocess(@RequestParam("id") Optional<Integer> id) {
//        //id=Optional.of(1);
//        SearchParameter<CashVo> parameter = new SearchParameter<>();
//        CashVo  cashVo;
//        if (id.isPresent()) {
//            cashVo = cashWithdrawalHandler.findByIdVo(id.get());
//        }else {
//            cashVo=new CashVo();
//        }
//        parameter.setEntity(cashVo);
//        return parameter;
//    }

    @RequestMapping(value = "/edit.html")
    public String edit_enter(
            @ModelAttribute("parameter") SearchParameter<CashWithdrawal> parameter,
            HttpServletRequest request, Model model) {
        try {
            if (parameter.getEntity().getId() != null) {
                model.addAttribute("action", "修改");
            } else {
                model.addAttribute("action", "新增");
            }
        } catch (Exception exp) {
            exp.printStackTrace();
        }
        return "pyramid/cash_edit1";
    }

    @ModelAttribute("parameter")
    public SearchParameter<CashWithdrawal> preprocess(@RequestParam("id") Optional<Integer> id) {
        SearchParameter<CashWithdrawal> parameter = new SearchParameter<>();
        CashWithdrawal  cashWithdrawal;
        if (id.isPresent()) {
            cashWithdrawal = cashWithdrawalHandler.findById(id.get());
        }else {
            cashWithdrawal=new CashWithdrawal();
        }
        parameter.setEntity(cashWithdrawal);
        return parameter;
    }

    @ResponseBody
    @RequestMapping(value = "/encrypt.json", method = RequestMethod.POST)
    public Message encrypt(String encryptLotteryCode) {
        byte[] b = null;
        String s = null;
        try {
            b = encryptLotteryCode.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b != null) {
            s = new BASE64Encoder().encode(b);
        }
        return Message.createSuccessMessage("success",s);
    }

}
